<template>
  <el-dialog
    title="删除站点"
    :visible.sync="deleteSiteDialog"
    width="681px"
    class="dialogTitle"
    @close="close"
  >
    <div class="deleteTip1" v-html="msg1 + this.currentSelect + msg2"></div>
    <div class="deleteTip2">确定<b>删除</b>选中的站点？</div>
    <div class="dialog-footer">
      <cs-btn
        type="primary"
        @click.native="deleteBtnClick"
        :buttonStatus="buttonStatus"
        btnText="确定"
      />
      <cs-btn @click.native="deleteSiteDialog = false" btnText="取消" />
    </div>
  </el-dialog>
</template>

<script>
import csBtn from '@/components/cs-btn/cs-btn.vue'

import { deleteSiteByID } from '@/service/operate/site_manage/site_manage'

export default {
  components: {
    csBtn
  },
  data() {
    return {
      deleteSiteDialog: false,
      currentSelect: 1,
      delId: [],
      msg1: '您已选择<b>',
      msg2: '个</b>车辆',
      buttonStatus: false
    }
  },
  created() {
    this.$bus.$on('deleteSiteDialogVisible', (selectId, selectNum) => {
      console.log({ selectId, selectNum })
      this.delId = selectId
      if (selectNum) {
        this.currentSelect = selectNum
      }
      this.deleteSiteDialog = true
    })
    this.$bus.$on('siteSelectNum', (selectNum) => {
      this.currentSelect = selectNum
    })
  },
  beforeDestroy() {
    this.$bus.$off('delSiteSuccess')
  },
  methods: {
    close() {},
    async deleteBtnClick() {
      this.buttonStatus = true
      try {
        const { code, data } = await deleteSiteByID(this.delId)
        console.log(data)
        if (code === 0) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.$bus.$emit('delSiteSuccess')
          this.deleteSiteDialog = false
          this.buttonStatus = false
        } else {
          this.$message.error(data.msg)
          this.deleteSiteDialog = false
          setTimeout(() => {
            this.buttonStatus = false
          }, 500)
        }
      } catch (err) {
        console.log(err)
        this.$message.error('删除站点信息失败')
        this.buttonStatus = false
      }
    }
  }
}
</script>

<style scoped>
/* 对话框样式start */
.el-form-item >>> .el-form-item__label {
  padding: 0;
  padding-right: 46px;
}

.dialogTitle >>> .el-dialog {
  padding: 6px 10px 18px 10px !important;
}
/* 对话框样式end */
</style>
